﻿using AnlianCommon.Common.Domains.Addresses;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.CommonRegion
{
    public class CountyQueryHandler : IRequestHandler<CountyQueryCommand, Result>
    {
        private readonly IBaseRepository<County> _countyRepository;

        public CountyQueryHandler(IBaseRepository<County> countyRepository)
        {
            _countyRepository = countyRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(CountyQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Search};
            try
            {
                var county = await (await _countyRepository.GetAll()).Where(c => c.DirectlyId == request.DirectlyId).ToListAsync();
                result.Data = county;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
